﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VideoClassLibrary.BusinessLogicLayer;
using VideoClassLibrary.Model;
using System.IO;
public partial class SchoolManagement_UpdateSchool : AdminUserBase
{
    public int currentSchoolId = 0;
    public V_School currentSchool;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["SID"] != null)
        {
            currentSchoolId = int.Parse(Request.QueryString["SID"].ToString());
            SchoolModule sm = new SchoolModule();
            currentSchool = sm.GetSchoolModelById(currentSchoolId);
        }
        if (!IsPostBack)
        {
           
            BindSchoolInfo();
        }
      
    }

    private void BindSchoolInfo()
    {

        //txtSchoolType.Text = Common.GetSchoolTypeName(currentSchool.ID);
        rbtnSchoolType.SelectedValue= currentSchool.VS_type.ToString();
        txtVS_name.Text = currentSchool.VS_name;
        ddlSchoolState.SelectedValue = currentSchool.VS_state.ToString();
        txtVS_phone.Text = currentSchool.VS_phone;
        txtVS_englishname.Text = currentSchool.VS_englishname;
        txtVS_zipcode.Text = currentSchool.VS_zipcode;
        txtVS_city.Text = currentSchool.VS_city;
        txtVS_province.Text = currentSchool.VS_province;
        txtVS_relanameemail.Text = currentSchool.VS_relanameemail;
        txtVS_relanamephone.Text = currentSchool.VS_relanamephone;
        txtVS_relanname.Text = currentSchool.VS_relanname;
        txtdesc.Text = currentSchool.Vs_desc;
        txtVS_zipcode.Text = currentSchool.VS_zipcode;
        txtVS_phone.Text = currentSchool.VS_phone;
        txtVS_address.Text = currentSchool.VS_address;


        string dictionarySavedPath = PageHelper.GetWebConfigValue("VideoPath").Replace("SchoolId", currentSchoolId.ToString()) + currentSchool.VS_logofile;

        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "javascript:PreviewImg('.." + dictionarySavedPath + "');", true);

    }

        
      protected void Update_Click(object sender, EventArgs e)
      {
          string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);
          if (fileExtension.ToLower() != "jpg" && fileExtension.ToLower() == "gif")
          {
              PageHelper.AlterMsg("图片格式只支持jpg/gif");
              return;
          }
          string dictionarySavedPath = Server.MapPath(PageHelper.ApplicationPath) + PageHelper.GetWebConfigValue("VideoPath").
          Replace("SchoolId",currentSchoolId.ToString());//保存logo的文件夹绝对路径
  
          string fileSavedPath = dictionarySavedPath + "/logo" + fileExtension;
          FileUpload1.SaveAs(fileSavedPath);

        
          currentSchool.update_time = DateTime.Now;
          currentSchool.VS_address = txtVS_address.Text;
          currentSchool.VS_city = txtVS_city.Text;
          currentSchool.VS_englishname = txtVS_englishname.Text;
          currentSchool.VS_logofile = "logo" + fileExtension;
          currentSchool.VS_name = txtVS_name.Text;
          currentSchool.VS_phone = txtVS_phone.Text;
          currentSchool.VS_province = txtVS_province.Text;
          currentSchool.VS_relanameemail = txtVS_relanameemail.Text;
          currentSchool.VS_relanamephone = txtVS_relanamephone.Text;
          currentSchool.VS_relanname = txtVS_relanname.Text;
          currentSchool.Vs_desc = txtdesc.Text;
          currentSchool.VS_state = Convert.ToInt32(ddlSchoolState.SelectedValue.Trim());
          currentSchool.VS_type = int.Parse(rbtnSchoolType.SelectedValue.Trim());
          currentSchool.VS_zipcode = txtVS_zipcode.Text;
          SchoolModule sm = new SchoolModule();
          int result = sm.UpdateSchool(currentSchool);
          if (result > 0)
          {

              PageHelper.AlterMsgAndRedirect("修改成功！", "SearchAndSchool.aspx");
          }
          else
          { PageHelper.AlterMsg("修改失败！"); }


      }
}
